<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-30
 * Time: 下午2:15
 */
namespace V1;
class C_Collection extends \ResourceClass {
    public $isActive = true;
    public $remark = '用户收藏商品';

    public function put(array $req, array $post, &$result){
        try {
            /* 授权验证【开始】 */
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            if (!array_key_exists('payload',$post)) {
                return 'require payload';
            }

            /* 授权验证【结束】 */
            $data = base64_decode($post['payload']);
            @parse_str($data,$data);

            // type 1收藏0取消收藏
            if (isset($data['enterprise_id']) == false
                || isset($data['channel_id']) == false
                || isset($data['product_id']) == false
                || isset($data['type']) == false ){
                return 'pramater requied';
            }
           if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'],$data['enterprise_id'], $req['t'].$post['payload'], $req['k']) == false) {
                return 'data illegal';
            }
            // 第一层判断产品,用户,渠道是否统一 （该用户收藏的是否是他所在渠道的商品）
            $customer_info = \Biz\EnterpriseCustomerHandler::getEnterpriseCustomerById($data['enterprise_id'],$req['uid']);
            $customer_channel_id = $customer_info->getChannelId();
            $customer_enterprise_id = $customer_info->getEnterpriseId();

            if ($customer_channel_id != $data['channel_id']) {    //验证用户渠道
                return 'invalid channel id ';
            }
            if ($customer_enterprise_id != $data['enterprise_id']) {//验证用户企业
                return 'invalid enterprise id ';
            }
            //查询当前渠道在不在该商品
            $talabtion = new \Cls\Tabulation();
            $talabtion -> setWhere('product_id',$data['product_id']);
            $talabtion -> setWhere('channel_id',$data['channel_id']);
            $talabtion->setLimit(9999);
            $product_hadler = new \Biz\ProductHandler();
            $product_hadler -> getProductChannelList($talabtion);
            if(!$talabtion->getSuccessStatus()){
                return 'product requied';
            }
            // 根据用户id渠道id取消收藏，或收藏某渠道下的产品
            if ($data['type'] == 1) {
                $customer_collection_handler = new \Biz\CustomerCollectionHandler();
                $talabtion_collection = new \Cls\Tabulation();
                $talabtion_collection -> setWhere('customer_id',$req['uid']);
                $talabtion_collection -> setWhere('channel_id',$data['channel_id']);
                $talabtion_collection -> setWhere('product_id',$data['product_id']);
                $talabtion_collection -> setWhere('is_del',0);
                $talabtion_collection->setLimit(9999);
                $customer_collection_handler ->getCustomerCollectionList($talabtion_collection);
                $has_product = $talabtion_collection->getData();
                if($has_product){
                        $customer_collection_cls = new \Cls\CustomerCollection\CustomerCollection();
                        //创建收藏基础数据
                        $customer_collection_cls -> crete($req['uid'],$data['channel_id'],$data['product_id']);
                        $customer_collection_cls ->setIsDel(\Cls\CustomerCollection\CustomerCollection::DELETE_STATUS);
                        $res =  $customer_collection_handler ->update($customer_collection_cls);
                } else {
                        $customer_collection_cls = new \Cls\CustomerCollection\CustomerCollection();
                        //创建收藏基础数据
                        $customer_collection_cls -> crete($req['uid'],$data['channel_id'],$data['product_id']);
                        $customer_collection_cls -> setCollectiontime(date('Y-m-d H:i:s'));
                        $customer_collection_cls ->setIsDel(\Cls\CustomerCollection\CustomerCollection::DELETE_STATUS);
                        $relult = $customer_collection_handler -> insert($customer_collection_cls);
                }
            } else if ($data['type'] == 0) {
                    $talabtion = new \Cls\Tabulation();
                    $customer_collection_handler = new \Biz\CustomerCollectionHandler();
                    //创建收藏基础数据
                    $talabtion->setWhere('customer_id',$req['uid'])
                        ->setWhere('product_id',$data['product_id'])
                        ->setWhere('is_del',1)
                        ->setWhere('channel_id',$data['channel_id']);
                    $customer_collection = $customer_collection_handler->getCustomerCollectionList($talabtion);
                    foreach($customer_collection->getData() as $customer_collection_cls){
                        $customer_collection_cls ->setIsDel(\Cls\CustomerCollection\CustomerCollection::NORMAL_STATUS);
                        $res =  $customer_collection_handler ->updateIsdel($customer_collection_cls);
                    }
            }
            return '';
        } catch (Exception $e) {
            return $e->getMessage();
        }

    }
}